//
//  UIViewController2.m
//  FingerChinese
//
//  Created by luar on 21/02/2010.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "UIViewController2.h"
#import "UIPageScrollView.h"
#import "NavigationTitle.h"

@implementation UIViewController2

@synthesize pickerViewArray, isFingerPage, initPageNo, flashCardPageNo, fingerPageNo, isHorizontalScroll;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		initPageNo = -1;
		isHorizontalScroll = YES;
		pickerViewArray = [[NSMutableArray array] retain];
	}
	return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	CGRect frame = [UIScreen mainScreen].bounds;
	self.view = [[[UIView alloc] initWithFrame:frame] autorelease];
	_scrollView = [[UIPageScrollView alloc] initWithFrame:frame];
	_scrollView.host = self;
	_scrollView.isHorizontalScroll = isHorizontalScroll;
	[self.view addSubview:_scrollView];

	UISegmentedControl *leftSeg = [UISegmentedControl alloc];
	if (isFingerPage) {
		if (flashCardPageNo > 0) {
			[[leftSeg initWithItems:
			  [NSArray arrayWithObjects:
			   [UIImage imageNamed:@"menu_icon_menu.png"],
			   [UIImage imageNamed:@"menu_icon_flashcard.png"],
			   nil]] autorelease];
		} else {
			[[leftSeg initWithItems:
			  [NSArray arrayWithObjects:
			   [UIImage imageNamed:@"menu_icon_menu.png"],
			   nil]] autorelease];
		}		
	} else {
		if (fingerPageNo > 0) {
			[[leftSeg initWithItems:
			  [NSArray arrayWithObjects:
			   [UIImage imageNamed:@"menu_icon_menu.png"],
			   [UIImage imageNamed:@"menu_icon_finger.png"],
			   nil]] autorelease];
		} else {
			[[leftSeg initWithItems:
			  [NSArray arrayWithObjects:
			   [UIImage imageNamed:@"menu_icon_menu.png"],
			   nil]] autorelease];
		}		
	}
	
	[leftSeg addTarget:self action:@selector(leftSegClick:) forControlEvents:UIControlEventValueChanged];
	UIBarButtonItem *leftBarItem = [[[UIBarButtonItem alloc] initWithCustomView:leftSeg] autorelease];
	self.navigationItem.leftBarButtonItem = leftBarItem;
	
	UISegmentedControl *rightSeg;
	if (isHorizontalScroll) {
		rightSeg = [[[UISegmentedControl alloc] initWithItems:
					 [NSArray arrayWithObjects:
					  [UIImage imageNamed:@"menu_icon_left.png"],
					  [UIImage imageNamed:@"menu_icon_right.png"],
					  nil]] autorelease];
	} else {
		rightSeg = [[[UISegmentedControl alloc] initWithItems:
					 [NSArray arrayWithObjects:
					  [UIImage imageNamed:@"menu_icon_up.png"],
					  [UIImage imageNamed:@"menu_icon_down.png"],
					  nil]] autorelease];
	}
	[rightSeg addTarget:self action:@selector(rightSegClick:) forControlEvents:UIControlEventValueChanged];
	leftSeg.segmentedControlStyle = rightSeg.segmentedControlStyle = UISegmentedControlStyleBar;
	leftSeg.opaque = rightSeg.opaque = YES;
	leftSeg.momentary = rightSeg.momentary = YES;
	
	UIBarButtonItem *rightBarItem = [[[UIBarButtonItem alloc] initWithCustomView:rightSeg] autorelease];
	self.navigationItem.rightBarButtonItem = rightBarItem;

	title = [[[NavigationTitle alloc] initWithFrame:CGRectMake(0, 0, 120, 32)] autorelease];
	title.isFingerPage = isFingerPage;
	self.navigationItem.titleView = title;
	
	picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
	picker.showsSelectionIndicator = YES;
	picker.delegate = self;
	picker.dataSource = self;
	picker.hidden = YES;
	[picker setSoundsEnabled:NO];
	[self.view addSubview:picker];
	[picker release];
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNavigationTitleTouch:) name:@"NavigationTitleTouch" object:nil];
}
	
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	[self updateInterfaceOrientation];
	return YES;
}

- (void)updateInterfaceOrientation {
	[_scrollView updateInterfaceOrientation];
	CGSize pickerSize = [picker sizeThatFits:CGSizeZero];
	int pickerY2;
	if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
		pickerY = [UIScreen mainScreen].bounds.size.width-pickerSize.height/2;
		pickerY2 = (picker.hidden) ? 1000 : [UIScreen mainScreen].bounds.size.width-pickerSize.height;
		picker.frame = CGRectMake(0.0, pickerY2, [UIScreen mainScreen].bounds.size.height, pickerSize.height);
	} else {
		pickerY = [UIScreen mainScreen].bounds.size.height-pickerSize.height/2;
		pickerY2 = (picker.hidden) ? 1000 : [UIScreen mainScreen].bounds.size.height-pickerSize.height;
		picker.frame = CGRectMake(0.0, pickerY2, [UIScreen mainScreen].bounds.size.width, pickerSize.height);
	}
	NSLog(@"picker isShow:%i, w:%f h:%f", !picker.hidden, pickerSize.width, pickerSize.height);	
}

- (void)dealloc {
	[pickerViewArray release];
    [super dealloc];
}

#pragma mark -
#pragma mark UIPageScrollView

- (int)getPageNo {
	return _scrollView.pageNo;
}

- (void)jumpPage:(int)pageNo{
	[_scrollView jumpPage:pageNo];
}

- (void)jumpPageDid:(int)_pageNo pageQuantity:(int)pageQuantity {
	[title setTitle:[NSString stringWithFormat:@"%i / %i", _pageNo, pageQuantity]];
}

#pragma mark -
#pragma mark UINavigationBar Event

- (void)leftSegClick:(id)sender {
	if (!picker.hidden) {
		[self showHidePicker:NO];
	}
	if([sender selectedSegmentIndex] == 0) {
		[self.navigationController popViewControllerAnimated:YES];
	} else if ([sender selectedSegmentIndex] == 1) {
		[self.navigationController popViewControllerAnimated:NO];
		[[NSNotificationCenter defaultCenter] postNotificationName:@"MenuClick" object:self];
	}
}

- (void)rightSegClick:(id)sender {
	if (!picker.hidden) {
		[self showHidePicker:NO];
	}	
	if([sender selectedSegmentIndex] == 0) {
		[_scrollView movePage:-1];
	} else if ([sender selectedSegmentIndex] == 1) {
		[_scrollView movePage:1];
	}
}

- (void)onNavigationTitleTouch:(NSNotification *)notification {
	NavigationTitle *navTitle = [notification object];
	if (navTitle.isFingerPage == isFingerPage) {
		NSLog(@"NavigationTitle touchesEnded %i", navTitle.isFingerPage);
		[self showHidePicker:picker.hidden];
	}
}


#pragma mark -
#pragma mark UIPickerView

- (void)showHidePicker:(BOOL)isShow {
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.3];
	if(isShow) { // off screen
		picker.hidden = NO;
		picker.center = CGPointMake(picker.center.x, pickerY);
		[picker selectRow:_scrollView.pageNo inComponent:0 animated:NO];
		[picker setSoundsEnabled:YES];
		[[NSNotificationCenter defaultCenter] removeObserver:self name:@"PageTouchesBegan" object:nil];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pageTouchesBegan:) name:@"PageTouchesBegan" object:nil];
	} else {
		[picker setSoundsEnabled:NO];
		picker.center = CGPointMake(picker.center.x, pickerY*2);
		[UIView setAnimationDelegate:self];
		[UIView setAnimationDidStopSelector:@selector(onPickerHide:finished:context:)];
	}
	[UIView commitAnimations];
}

- (void)pageTouchesBegan:(NSNotification *)notification {
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"PageTouchesBegan" object:nil];
	[self showHidePicker:NO];
}

- (void)onPickerHide:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
	picker.hidden = YES;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	[_scrollView jumpPage:row];
	//[self showHidePicker:NO];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
	return 120;//self.view.frame.size.width;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
	return _scrollView.pageQuantity;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
	return 1;
}

#pragma mark -
#pragma mark UIPickerView dataSource

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	return [pickerViewArray objectAtIndex:row];
}

@end
